<template lang="pug">
  view-box.invitpage
    //- h1 {{ msg }}
    .invit(@click="enter" v-show="!confirm")
      p {{user}}
      p 邀请你加入 {{union}}
    .invit.ok( v-show="confirm")
</template>

<script>
import memberService from '@/api/member'
const wechatConfig = require('../../wechatConfig')
import axios from 'axios'
export default {
  name: 'invitpage',
  data () {
    return {
      user: '测试人员1',
      union: '我的工会',
      confirm: false,
      locationQuery:{}
    }
  },
  created(){
    document.title = '邀请函'
  },
  methods: {
    checkIsReg (data) {
      if (data.scope === 'snsapi_base') {
        homeService.IsRegister({openid: data.openid})
        .then(res => {
          // localStorage.setItem('openid', res.result.uid)
          console.log('IsRegister', res)
          if (res.result.isreg) {
            this.$vux.alert.show({
              title: '您已注册',
              onHide () {
                
              }
            })
          } else {
            router.replace('/licence')
          }
        })
      } else {
        // 获取用户详细信息 并注册
        console.log('snsapi_userinfo', data)
        var url = 'wechat/userinfo?access_token=' + data.access_token + '&openid=' + data.openid + '&lang=zh_CN'
        axios.get(url).then(user => {
          console.log('userInfo:', user.data)
          // debugger
          var params = user.data
          params.avatar = params.headimgurl
          homeService.Register(params).then(res => {
            homeService.Login()
              .then(res => {
                localStorage.setItem('uid', res.result.uid)
                router.replace('/index')
              })
          })
        })
      }
    },
    enter () {
      var apiUrl = '/wechat/oauth2/access_token?appid=' + wechatConfig.appID + '&secret=' + wechatConfig.appScrect + '&code=' + locationQuery.code + '&grant_type=authorization_code'
      axios.get(apiUrl).then(d => {
        console.log('access_token:', d)
        var access_token = d.data.access_token
        var openid = d.data.openid
        localStorage.setItem('openid', d.data.openid)
        // 3 检查是否注册
        if (localStorage.getItem('jwtToken')) {
          this.checkIsReg(d.data)
        } else {
          tokenService.AccessToken()
            .then(res => {
              // console.log(res)
              localStorage.setItem('jwtToken', res.result.token)
              this.checkIsReg(d.data)
            })
        }
      })
    },
    getinviteUserInfo(){
      this.locationQuery = {}
      window.location.search.slice(1).split('&').forEach(d => {
        var p = d.split('=')
        this.locationQuery[p[0]] = p[1]
      })
      
      memberService.MemberInfo({
        openid: this.locationQuery.state
      }).then(usr =>{
        // console.log('usr', usr)
        this.user =  usr.result.nickname
        memberService.GuildInfo({uid:usr.result.uid})
        .then(res => {
          if(res.status === 0){
            // this.hasUnion = false
          }else{
            this.union = res.result.name
          }
        })
        this.$forceUpdate()
      })
       
    }
  },
  mounted () {
    
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less">
  .invitpage{
    text-align: center;
    padding:40px;
    background-image: url(../assets/bg.jpg);
    background-size: 100% 100%;
    .invit{
      display: inline-block;
      width:300px;
      height: 490px;
      position: absolute;
      top:50%;
      left: 50%;
      margin-left: -150px;
      margin-top:-260px;
      background-image: url(../assets/invitCard.png);
      background-size: 100% 100%;
      color: #f90;
      padding-top:150px;
      font-size: 24px;
      &.ok{
        background-image: url(../assets/invitSuccess.png);
        background-size: 100% 100%;
      }
    }
  }
  
</style>
